/*
NOTES TO DATA ANALYSIS:
	2. Trends in treatment assignment by rounds
		- Stacked bar chart: each bar represents a round 
		- Table: round/date as row and treatment arms as column (10)
*/

clear all
set more off
capture log close

********************************************************************************

log using "$logfiles/04_analysis_treatmentshares_`c(current_date)'.log", replace

use "$covidclean/smscovid_clean.dta", clear

***Keep relevant observations: consent taken and above 18 years of age, non-missing treatment arms
keep if consent==1 & age>=18
keep if ~missing(treatment_arm)
		
********************************************************************************
***TRENDS IN TREATMENT ASSIGNMENTS BY ROUNDS
********************************************************************************
	
	***Adpative treatment shares by round
		
			use "$treatment/bihar_actualsample_trial_deidentified_complete_25Sept20", clear
			tab behavior dateofdelivery, mi
			levelsof dateofdelivery, local(dates)
			local round : word count `dates'
			gen round_sd = .
			gen round_hw = .
			forval i = 1/`round' {
				local date : word `i' of `dates'
				di `date'
				replace round_sd = `i' if behavior == 1 & dateofdelivery == `date'
				replace round_hw = `i' if behavior == 2 & dateofdelivery == `date'
			}	
			tab behavior round_sd if behavior==1
			tab behavior round_hw if behavior==2
			
			duplicates drop behavior dateofdelivery round_sd round_hw, force
			keep behavior dateofdelivery round_sd round_hw
			
			tempfile round
			save `round', replace
		
	
		frame create adaptiveshares
		frame change adaptiveshares
		
			clear
			cd "$treatment"
			local filelist: dir . files "*_t_rates.dta"
			local j = 1
			foreach file in `filelist' {
				tempfile file`j'
				use `file', clear
				gen treatmentfilename = "`file'"
				save `file`j''
				local ++ j
			}
			clear

			local j = `j' - 1
				forval i = 1/`j' {
				append using `file`i''
			}

			unique treatmentfilename 
			tab treatmentfilename, mi

			gen date = substr(treatmentfilename,1,10)
			gen dateofdelivery = date(date, "YMD")
			tab dateofdelivery, mi
				replace dateofdelivery = td(22aug2020) if dateofdelivery == td(19aug2020)
				replace dateofdelivery = td(23aug2020) if dateofdelivery == td(20aug2020)
				replace dateofdelivery = td(27aug2020) if dateofdelivery == td(26aug2020)
				replace dateofdelivery = td(26aug2020) if dateofdelivery == td(25aug2020)
				replace dateofdelivery = td(01sep2020) if dateofdelivery == td(30aug2020)
				replace dateofdelivery = td(30aug2020) if dateofdelivery == td(29aug2020)
				
				replace dateofdelivery = td(05sep2020) if dateofdelivery == td(04sep2020)
				replace dateofdelivery = td(04sep2020) if dateofdelivery == td(03sep2020)
				replace dateofdelivery = td(09sep2020) if dateofdelivery == td(08sep2020)
				replace dateofdelivery = td(08sep2020) if dateofdelivery == td(07sep2020)
				replace dateofdelivery = td(13sep2020) if dateofdelivery == td(12sep2020)
				replace dateofdelivery = td(12sep2020) if dateofdelivery == td(11sep2020)
				replace dateofdelivery = td(18sep2020) if dateofdelivery == td(15sep2020)
				replace dateofdelivery = td(19sep2020) if dateofdelivery == td(16sep2020)
				replace dateofdelivery = td(23sep2020) if dateofdelivery == td(22sep2020)
				replace dateofdelivery = td(22sep2020) if dateofdelivery == td(21sep2020)
				replace dateofdelivery = td(27sep2020) if dateofdelivery == td(26sep2020)
				replace dateofdelivery = td(26sep2020) if dateofdelivery == td(25sep2020)
				
				replace dateofdelivery = td(04oct2020) if dateofdelivery == td(30sep2020)
				replace dateofdelivery = td(03oct2020) if dateofdelivery == td(29sep2020)
				replace dateofdelivery = td(08oct2020) if dateofdelivery == td(07oct2020)
				replace dateofdelivery = td(07oct2020) if dateofdelivery == td(06oct2020)
				replace dateofdelivery = td(12oct2020) if dateofdelivery == td(11oct2020)
				replace dateofdelivery = td(11oct2020) if dateofdelivery == td(10oct2020)
				replace dateofdelivery = td(16oct2020) if dateofdelivery == td(15oct2020)
				replace dateofdelivery = td(15oct2020) if dateofdelivery == td(14oct2020)

			merge m:1 dateofdelivery using `round'
			format dateofdelivery %td
			drop if dateofdelivery==td(16aug2020) & _merge==2
			drop if dateofdelivery==td(17aug2020) & _merge==2
			drop if _merge != 3 
			sort dateofdelivery
			drop _merge
			
			levelsof round_sd, local(roundtotal)
			local round : word count `roundtotal'
			forval i = 1/`round' {
				local round_sd : word `i' of `roundtotal'
				local i = `i' + 1
				la def round_sd `round_sd' "Round `i'", modify
			}
			la val round_sd round_sd
			tab round_sd, mi
			
			levelsof round_hw, local(roundtotal)
			local round : word count `roundtotal'
			forval i = 1/`round' {
				local round_hw : word `i' of `roundtotal'
				local i = `i' + 1
				la def round_hw `round_hw' "Round `i'", modify
			}
			la val round_hw round_hw
			tab round_hw, mi
			
			insobs 10 
			replace round_sd = 0 if missing(round_sd) & missing(rate)
			replace rate = 0.10 if missing(rate) & round_sd==0
			gsort round_sd treatment
			forval i = 1/10 {
				replace treatment = `i' in `i' 
			}
			la def round_sd 0 "Round 1", modify
			
			insobs 10 
			replace round_hw = 0 if missing(round_hw) & missing(rate)
			replace rate = 0.10 if missing(rate) & round_hw==0
			gsort round_hw treatment
			forval i = 1/10 {
				replace treatment = `i' in `i' 
			}
			la def round_hw 0 "Round 1", modify
			
			*Pool Rounds 0 to 5 as one round, addressing the adaptive sampling mistake (in the first five rounds) in assignment of SMS
			gen round_sd_pool = round_sd
			replace round_sd_pool = 1 if inlist(round_sd,0,3,5,7,9)
			recode round_sd_pool (11=2) (13=3) (15=4) (17=5) (19=6) (21=7) (23=8) (25=9) (27=10)
			tab round_sd_pool round_sd
			
			gen round_hw_pool = round_hw
			replace round_hw_pool = 1 if inlist(round_hw,0,4,6,8,10)
			recode round_hw_pool (12=2) (14=3) (16=4) (18=5) (20=6) (22=7) (24=8) (26=9) (28=10)
			tab round_hw_pool round_hw

			***Graph treatment assignments by round for each behavior
			
				*Global for legends
				global legend_line "label(1 Neutral - twice morning) label(2 Neutral - morning/evening) label(3 Public gain - twice morning) label(4 Public gain - morning/evening) label(5 Public loss - twice morning) label(6 Public loss - morning/evening) label(7 Private gain - twice morning) label(8 Private gain - morning/evening) label(9 Private loss - twice morning) label(10 Private loss - morning/evening)"
				global rtitles "Neutral (morn.) \ Public gain (morn.) \ Public loss (morn.) \ Private gain (morn.) \ Private loss (morn.) \ Neutral (morn./eve.) \ Public gain (morn./eve.) \ Public loss (morn./eve.) \ Private gain (morn./eve.) \ Private loss (morn./eve.)"
				global ctitles " "", Round, "", "", "", "", "", "", "", "", "", "", "", "", "" \ "", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14"
				global ctitles_pool " "", Round, "", "", "", "", "", "", "", "", "", "" \ "", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11"
				global font "basefont(footnotesize) statfont(footnotesize; footnotesize; footnotesize; footnotesize) rtitlfont(footnotesize; footnotesize; footnotesize; footnotesize) ctitlfont(footnotesize) notefont(scriptsize)"
				 
				*Pool Rounds 1 to 5 as one round, addressing the adaptive sampling mistake in assignment of SMS
				preserve 
				
					keep if ~missing(round_sd_pool)
					collapse (mean) rate, by(round_sd_pool treatment)
					tsset treatment round_sd_pool
					graph tw (tsline rate if treatment==1, lcolor(gray) lp(solid)) ///
							 (tsline rate if treatment==6, lcolor(gray) lp(dash)) ///
							 (tsline rate if treatment==2, lcolor(navy) lp(solid)) ///
							 (tsline rate if treatment==7, lcolor(navy) lp(dash)) ///
							 (tsline rate if treatment==3, lcolor(red) lp(solid)) ///
							 (tsline rate if treatment==8, lcolor(red) lp(dash)) ///
							 (tsline rate if treatment==4, lcolor(green) lp(solid)) ///
							 (tsline rate if treatment==9, lcolor(green) lp(dash)) ///
							 (tsline rate if treatment==5, lcolor(orange) lp(solid)) ///
							 (tsline rate if treatment==10, lcolor(orange) lp(dash)), ///
					   ytitle("Treatment assignment probability", size(small)) xtitle(" ") title("Distancing", size(medium)) ///
					   legend($legend_line symysize(vsmall) symxsize(*0.3) size(vsmall) cols(2) rowgap(0.5pt)) ///
					   graphregion(color(white)) bgcolor(white) xlabel(1(1)10, labsize(small)) ylabel(0(0.1)0.5, labsize(small) nogrid) ///
					   saving("$figures/share_sd_pool", replace) name(share_sd_pool, replace)
					graph export "$figures/share_sd_pool.eps", as(eps) replace
					   
				 restore 
				 
				 preserve 
				
					keep if ~missing(round_hw_pool)
					collapse (mean) rate, by(round_hw_pool treatment)
					tsset treatment round_hw_pool
					graph tw (tsline rate if treatment==1, lcolor(gray) lp(solid)) ///
							 (tsline rate if treatment==6, lcolor(gray) lp(dash)) ///
							 (tsline rate if treatment==2, lcolor(navy) lp(solid)) ///
							 (tsline rate if treatment==7, lcolor(navy) lp(dash)) ///
							 (tsline rate if treatment==3, lcolor(red) lp(solid)) ///
							 (tsline rate if treatment==8, lcolor(red) lp(dash)) ///
							 (tsline rate if treatment==4, lcolor(green) lp(solid)) ///
							 (tsline rate if treatment==9, lcolor(green) lp(dash)) ///
							 (tsline rate if treatment==5, lcolor(orange) lp(solid)) ///
							 (tsline rate if treatment==10, lcolor(orange) lp(dash)), ///
					   ytitle("Treatment assignment probability", size(small)) xtitle("Experiment round") title("Handwashing", size(medium)) ///
					   legend($legend_line symysize(vsmall) symxsize(*0.3) size(vsmall) cols(2) rowgap(0.5pt)) ///
					   graphregion(color(white)) bgcolor(white) xlabel(1(1)10, labsize(small)) ylabel(0(0.1)0.5, labsize(small) nogrid) ///
					   saving("$figures/share_hw_pool", replace) name(share_hw_pool, replace )
					graph export "$figures/share_hw_pool.eps", as(eps) replace
					   
				 restore
				 
				 grc1leg share_sd_pool share_hw_pool, legendfrom(share_hw_pool) rows(2) graphregion(color(white)) 
				 graph export "$figures/share_pool.eps", as(eps) replace
				
log close
exit, clear
